import {Tools, Modal, Toast} from 'weaver-mobile';
import querystring from 'querystring';

let modalInstance = null;
//弹确认框
export const showConfirm = (obj = {}, hasCancel = true) => {
    const {getLabel} = Tools;
    /**
     * 默认的参数
     */
    const def = {
        title: getLabel(131329, '信息确认'),
        content: "",
        cancelText: getLabel(201, '取消'),
        okText: getLabel(826, '确定'),
        cancelEvent: () => {
        },
        okEvent: () => {
        }
    };

    const params = {...def, ...obj};
    let btns = [];
    if (hasCancel) {
        btns.push({
            text: params.cancelText,
            onPress: () => {
                typeof params.cancelEvent === 'function' && params.cancelEvent();
                modalInstance = null;
            },
            style: 'default'
        });
    }
    btns.push({
        text: params.okText,
        onPress: () => {
            typeof params.okEvent === 'function' && params.okEvent();
            modalInstance = null;
        }
    });
    modalInstance = Modal.alert(params.title, params.content, btns);
}

export const closeConfirmModal = () => {
    if (!!modalInstance) {
        modalInstance.close();
        modalInstance = null;
    }
}

export const queryUrlString = () => {
    const search = window.location.search.replace('?', '');
    const hashParams = window.location.hash.indexOf('?') >= 0 ? window.location.hash.split('?')[1] : '';

    return {
        ...querystring.parse(hashParams), // 兼容 #/xxx/xxx?x=x的方式
        ...querystring.parse(search),
    };
}

//初始化按钮位置
export const initSignApprovalButtonPosition = ($dom) => {
    let winHeight = document.body.offsetHeight;
    if (window.localStorage.getItem("hqButtonPosition")) {
        let hqButtonPosition = JSON.parse(window.localStorage.getItem("hqButtonPosition"));
        $dom.style.top = hqButtonPosition.buttonTop;
        $dom.style.left = hqButtonPosition.buttonLeft;
        if (hqButtonPosition.buttonLeft !== "0px") {
            $dom.style.borderRadius = "25px 0 0 25px";
        } else {
            $dom.style.borderRadius = "0px 25px 25px 0px";
        }
    } else {
        $dom.style.top = (winHeight - 200) + "px";
        $dom.style.left = "0px";
        $dom.style.borderRadius = "0px 25px 25px 0px";
    }
};

export const initSignApprovalFloatDom = ($dom, callback, params) => {
    initSignApprovalButtonPosition($dom);
    let flag = 0; //标记是拖曳还是点击
    let disX = 0;
    let disY = 0;
    let L = 0;
    let T = 0;
    let starX = 0;
    let starY = 0;
    let starXEnd = 0;
    let moveX = 0;
    let moveY = 0;
    let starYEnd = 0;
    let minBottom = 0;
    if ((params.signDeviceType && params.signDeviceType == 1) || !window.em) {
        minBottom = 50;
    }
    $dom.addEventListener('touchstart', function (e) {
        flag = 0;
        // e.preventDefault();//阻止触摸时页面的滚动，缩放
        disX = e.touches[0].clientX - this.offsetLeft;
        disY = e.touches[0].clientY - this.offsetTop;
        //手指按下时的坐标
        starX = e.touches[0].clientX;
        starY = e.touches[0].clientY;
    });
    $dom.addEventListener('touchmove', function (e) {
        e.preventDefault();
        flag = 1;
        L = e.touches[0].clientX - disX;
        T = e.touches[0].clientY - disY;
        //移动时 当前位置与起始位置之间的差值
        starXEnd = e.touches[0].clientX - starX;
        starYEnd = e.touches[0].clientY - starY;
        if (L < 0) {//限制拖拽的X范围，不能拖出屏幕
            L = 0;
        } else if (L > document.documentElement.clientWidth - this.offsetWidth) {
            L = document.documentElement.clientWidth - this.offsetWidth;
        }
        if (T < minBottom) {//限制拖拽的Y范围，不能拖出屏幕
            T = minBottom;
        } else if (T > document.documentElement.clientHeight - this.offsetHeight - minBottom) {
            T = document.documentElement.clientHeight - this.offsetHeight - minBottom;
        }
        moveX = L + 'px';
        moveY = T + 'px';
        this.style.left = moveX;
        this.style.top = moveY;
    });
    $dom.addEventListener('touchend', function (e) {
        //判断滑动方向
        let winWidth = document.body.clientWidth;
        let left = parseInt(this.style.left.replace("px", ""));
        if (winWidth / 2 > left) {
            this.style.left = 0;
            this.style.borderRadius = "0 25px 25px 0";
        } else {
            this.style.left = (winWidth - 90) + "px";
            this.style.borderRadius = "25px 0 0 25px";
        }
        window.localStorage.setItem("hqButtonPosition", JSON.stringify({
            buttonLeft: this.style.left,
            buttonTop: this.style.top
        }));
    });
    $dom.addEventListener('click', function () {
        callback(params);
    });
}

export const initSignApprovalTouchMove = ($dom) => {
    let domWidth = parseInt($dom.style.width.replace("px", ""));
    let winWidth = document.body.clientWidth;
    if (domWidth <= winWidth) {
        $dom.style.left = "10px";
        return;
    }
    let maxWidth = winWidth - domWidth;
    let flag = 0; //标记是拖曳还是点击
    let disX = 0;
    let L = 0;
    let starX = 0;
    let starXEnd = 0;
    let moveX = 0;
    $dom.addEventListener('touchstart', function (e) {
        flag = 0;
        // e.preventDefault();//阻止触摸时页面的滚动，缩放
        disX = e.touches[0].clientX - this.offsetLeft;
        //手指按下时的坐标
        starX = e.touches[0].clientX;
    });
    $dom.addEventListener('touchmove', function (e) {
        e.preventDefault();
        domWidth = parseInt($dom.style.width.replace("px", ""));
        if (domWidth <= winWidth) {
            this.style.left = "10px";
            return;
        }
        maxWidth = winWidth - domWidth;
        flag = 1;
        L = e.touches[0].clientX - disX;
        //移动时 当前位置与起始位置之间的差值
        if (L >= 10) {
            L = 10;
        } else if (L <= maxWidth) {
            L = maxWidth;
        }
        moveX = L + 'px';
        this.style.left = moveX;
    });
}

/**
 * 流程方法:获取客户端类型 主要用于签字意见
 * 参见：src4js/mobile4mobx/workflowForm/util/SignUtil.js
 *
 * 返回值对应类型：
 * 参见：src/com/engine/workflow/constant/SignSource.java
 *  WEB_CLIENT("a", 83652, "来自Web手机版"),
 *  ANDROID_CLIENT("b", 83649, "来自android客户端"),
 *  IPHOE_CLIENT("c", 83643, "来自iPhone客户端"),
 *  PC_BROWSER("d", -1, "PC浏览器提交"),
 *  WECHAT_CLIENT("e", 507327, "来自企业微信"),
 *  DINGTALK_CLIENT("f", 507328, "来自钉钉客户端"),
 *  AUTO_APPROVE("g", 126802, "系统自动批准"),
 *  AUTO_SUBMIT("h", 126801, "系统自动提交"),
 *  WORKFLOW_PENETREATE("i",126802, "系统自动批准") //i 记录穿透
 * @returns {string}
 */
export const getClientTypeInfo = () => {
    const ua = Tools.mobileDetect().getUaInfo();
    const {isEmobile, mobile, os} = ua;
    let clienttype = "a";
    if (isEmobile) {
        if (os == 'AndroidOS') {
            clienttype = "b";
        } else {
            clienttype = "c";
        }
    } else {
        const userAgent = Tools.ua;
        const {browser} = userAgent;
        if ('Wechat' == browser) {
            clienttype = "e";
        } else if ('DingTalk' == browser) {
            clienttype = "f";
        }
    }
    return clienttype;
}


export const signapprovalMoveForIos = ($dom) => {
    $dom.addEventListener('touchstart', function () {
        document.querySelector(".ws-valid-scroll>div").style.left = 10 + "px";
    })

}